package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 会话资源控制器
 */
@RestController
@RequestMapping("/sessions")
public class SessionController {
    @Autowired
    private IUserService userService;

    /**
     * 用户登录资源
     * @param user 前端传入用户信息
     * @return 登录结果
     */
    @PostMapping
    public JSONResult login(User user) {
        JSONResult jsonResult = new JSONResult();
        try {
            Map<String, Object> map = userService.login(user);
            jsonResult.setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    @DeleteMapping
    public void logout() {
        UserContext.destroy();
    }
}
